package com.be.beadmin.product.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.product.dto.input.WrokingHoursDetailsDto;
import com.be.beadmin.product.dto.output.WrokingHoursDetailsEx;
import com.be.beadmin.product.service.WrokingHoursDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author zjm
* @date 2023-04-24
**/
@RestController
public class WrokingHoursDetailsController {

    @Autowired
    private  WrokingHoursDetailsService wrokingHoursDetailsService;


    @PostMapping(value = "/product/getWrokingHoursDetailsByPage")
    public ResultModel getWrokingHoursDetailsByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(wrokingHoursDetailsService.getWrokingHoursDetailsByPage(pageEntity)) ;
    }

    @PostMapping("/product/addWrokingHoursDetails")
    public ResultModel addWrokingHoursDetails(@RequestBody WrokingHoursDetailsDto wrokingHoursDetailsDto){
        wrokingHoursDetailsService.addWrokingHoursDetails(wrokingHoursDetailsDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/product/delWrokingHoursDetails")
    public ResultModel delWrokingHoursDetails(@RequestBody WrokingHoursDetailsDto wrokingHoursDetailsDto){
        wrokingHoursDetailsService.delWrokingHoursDetails(wrokingHoursDetailsDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/product/updateWrokingHoursDetails")
    public ResultModel updateWrokingHoursDetails(@RequestBody WrokingHoursDetailsDto wrokingHoursDetailsDto){
        wrokingHoursDetailsService.updateWrokingHoursDetails(wrokingHoursDetailsDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/product/exportWrokingHoursDetails")
    public void exportWrokingHoursDetails(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<WrokingHoursDetailsEx> wrokingHoursDetailsPage = wrokingHoursDetailsService.getWrokingHoursDetailsByPage(pageEntity);
        List<WrokingHoursDetailsEx> wrokingHoursDetailsExes = wrokingHoursDetailsPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("WrokingHoursDetails接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), WrokingHoursDetailsEx.class).sheet("sheet1").doWrite(wrokingHoursDetailsExes);
    }
}